package com.windfone.web.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.Constant;
import com.windfone.common.PageUtil;
import com.windfone.model.Gift;
import com.windfone.model.GiftUser;
import com.windfone.model.Member;
import com.windfone.model.Message;
import com.windfone.service.GiftService;
import com.windfone.service.MemberService;

public class GiftAdminAction extends ActionSupport {
	/** serialVersionUID	 */
	private static final long serialVersionUID = 1L;
	private GiftService giftService;
	private MemberService memberService;
	private List<Gift> giftList;
	private List<GiftUser> userAskgiftList;
	private int jifen;
	private int status;
	private int account;
	private String imagepath;
	private String imagepath2;
	private String imagepath3;
	private String imagepath4;
	public String getImagepath2() {
		return imagepath2;
	}
	public void setImagepath2(String imagepath2) {
		this.imagepath2 = imagepath2;
	}
	public List<GiftUser> getUserAskgiftList() {
		return userAskgiftList;
	}
	public void setUserAskgiftList(List<GiftUser> userAskgiftList) {
		this.userAskgiftList = userAskgiftList;
	}
	public String getImagepath3() {
		return imagepath3;
	}
	public void setImagepath3(String imagepath3) {
		this.imagepath3 = imagepath3;
	}
	public String getImagepath4() {
		return imagepath4;
	}
	public void setImagepath4(String imagepath4) {
		this.imagepath4 = imagepath4;
	}

	private String name;
	private int giftId;
	public int getGiftId() {
		return giftId;
	}
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	public void setGiftId(int giftId) {
		this.giftId = giftId;
	}
	public int getJifen() {
		return jifen;
	}
	public void setJifen(int jifen) {
		this.jifen = jifen;
	}
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	public int getAccount() {
		return account;
	}
	public void setAccount(int account) {
		this.account = account;
	}
	public String getImagepath() {
		return imagepath;
	}
	public void setImagepath(String imagepath) {
		this.imagepath = imagepath;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<Gift> getGiftList() {
		return giftList;
	}
	public void setGiftList(List<Gift> giftList) {
		this.giftList = giftList;
	}
	public void setGiftService(GiftService giftService) {
		this.giftService = giftService;
	}
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	public String addGift() throws Exception {
		bean = new Gift();
		bean.setName(name);
		bean.setAccount(account);
		bean.setImagepath(imagepath);
		bean.setImagepath2(imagepath2);
		bean.setImagepath3(imagepath3);
		bean.setImagepath4(imagepath4);
		bean.setJifen(jifen);
		bean.setStatus(1);//1 active 0 disable;
		giftService.addGift(bean);
		return SUCCESS;
	}
	
	public String beforeAddGift() throws Exception {

		return SUCCESS;
	}
	public String updateGift() throws Exception {
		bean = new Gift();
		bean.setName(name);
		bean.setAccount(account);
		bean.setImagepath(imagepath);
		bean.setImagepath2(imagepath2);
		bean.setImagepath3(imagepath3);
		bean.setImagepath4(imagepath4);
		bean.setJifen(jifen);
		bean.setId(giftId);
		bean.setStatus(1);//1 active 0 disable;
		giftService.updateGift(bean);
		return SUCCESS;
	}
	public String beforUpdateGift() throws Exception {
		Gift f = new Gift();
		f.setId(giftId);
		bean = giftService.getGift(f );
		return SUCCESS;
	}
	public String deleteGift() throws Exception {
		Gift f = new Gift();
		f.setId(giftId);
		giftService.delGift( f );
		return SUCCESS;
	}
	
	public Gift getBean() {
		return bean;
	}
	public void setBean(Gift bean) {
		this.bean = bean;
	}
	public int getBeginRecord() {
		return beginRecord;
	}
	public void setBeginRecord(int beginRecord) {
		this.beginRecord = beginRecord;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public int getNextPage() {
		return nextPage;
	}
	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getPrevPage() {
		return prevPage;
	}
	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}
	public GiftService getGiftService() {
		return giftService;
	}

	private Gift bean;
	private int beginRecord;
	private int currentPage;
	private int nextPage;
	private int totalPage;
	private int prevPage;
	public String giftList() throws Exception {
		String whereCondition = "";
		PageUtil pu = new PageUtil();
		pu.setCurrentPage(currentPage);
		int totalRecords = giftService.getAllRecorderNumber(whereCondition) ;
		pu.setTotalRecords( totalRecords );
		pu.setPageSize(Constant.PAGE_SIZE);
		pu.dealPage();
		currentPage = pu.getCurrentPage();
		nextPage = pu.getNextPage();
		totalPage = pu.getTotalPage();
		prevPage = pu.getPrevPage();
		
		beginRecord = Constant.PAGE_SIZE * (currentPage -1);
		if ( beginRecord< 0 )
			beginRecord = 0; 
		giftList = giftService.getAllGift(beginRecord,whereCondition);
		return SUCCESS;
	}
	
	private int userAskGifId;
	private int memberId;
	public int getMemberId() {
		return memberId;
	}
	public void setMemberId(int memberId) {
		this.memberId = memberId;
	}
	public int getUserAskGifId() {
		return userAskGifId;
	}
	public void setUserAskGifId(int userAskGifId) {
		this.userAskGifId = userAskGifId;
	}
	public String giftUserUpdate() throws Exception {
		GiftUser userAskBean  = new GiftUser();
		userAskBean.setId(userAskGifId);
		userAskBean.setStatus(1);
		giftService.updateUserAskGift(userAskBean);
		Message msgBean = new Message();
		msgBean.setMemberId(memberId);
		msgBean.setStatus(1);
		msgBean.setContent("已批准你对礼品的申请！");
		giftService.addMessage(msgBean );
		
		Gift f= new Gift();
		f.setId(giftId);
		Gift localBean = giftService.getGift( f );
		Member bean2 = memberService.getMemberByKey( memberId );
		int p = bean2.getPoints();
		p=p- localBean .getJifen();
		bean2.setPoints(p);
		memberService.updateMember(bean2); 
		return SUCCESS;
	}
	public String giftUserDelete() throws Exception {
		GiftUser userAskBean  = new GiftUser();
		userAskBean.setId(userAskGifId);
		giftService.deleteUserAskGift(userAskBean);
		return SUCCESS;
	}
	public String giftUserList() throws Exception {
		String whereCondition = "";
		PageUtil pu = new PageUtil();
		pu.setCurrentPage(currentPage);
		int totalRecords = giftService.getAllUserAskGiftRecorderNumber(whereCondition) ;
		pu.setTotalRecords( totalRecords );
		pu.setPageSize(Constant.PAGE_SIZE);
		pu.dealPage();
		currentPage = pu.getCurrentPage();
		nextPage = pu.getNextPage();
		totalPage = pu.getTotalPage();
		prevPage = pu.getPrevPage();
		
		beginRecord = Constant.PAGE_SIZE * (currentPage -1);
		if ( beginRecord< 0 )
			beginRecord = 0; 
		userAskgiftList = giftService.getAllUserAskGift(beginRecord,whereCondition);
		return SUCCESS;
	}

}
